package com.kaifamiao.month05.day09;

import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Demo03 {


    public static void main(String[] args) {
        ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
//        es.schedule(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(1000);
//                    System.out.println(Thread.currentThread().getName() + " , 执行时间：" + LocalTime.now());
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }, 3000, TimeUnit.MILLISECONDS); // 多少时间间隔之后执行线程任务

        es.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("执行时间: " + LocalTime.now());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, 1000, 2000, TimeUnit.MILLISECONDS); // 第一次任务在1秒之后执行，之后每隔2秒执行一次

    }
}
